package com.moji.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjProduct;
import com.moji.entity.MjSolution;
import com.moji.entity.dto.RouterrDto;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.mapper.MjSolutionMapper;
import com.moji.service.MjAboutUsService;
import com.moji.service.MjSolutionService;
import org.apache.http.util.TextUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 解决方案表(MjSolution)表服务实现类
 *
 * @author makejava
 * @since 2024-07-21 14:36:48
 */
@Service("mjSolutionService")
public class MjSolutionServiceImpl extends ServiceImpl<MjSolutionMapper, MjSolution> implements MjSolutionService {
    @Resource
    private MjSolutionMapper mjSolutionDao;

    /**
     * 通过ID查询单条数据
     *
     * @return 实例对象
     */
    @Override
    public MjSolution queryById(RouterrDto routerrDto) {
        if(routerrDto.getId()==null)
        {
           if(!TextUtils.isEmpty(routerrDto.getRouter()))
           {
               LambdaQueryWrapper<MjSolution> queryWrapper = new LambdaQueryWrapper<>();
               queryWrapper.eq(MjSolution::getRouter,routerrDto.getRouter());
               List<MjSolution> page = this.list(queryWrapper);
               if(page==null||page.size()!=1)
               {
                   return null;
               }else{
                   return  page.get(0);
               }

           }
        }
        return this.mjSolutionDao.selectById(routerrDto.getId());
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<MjSolution> queryByPage() {
        LambdaQueryWrapper<MjSolution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(MjSolution::getSort);
        queryWrapper.orderByDesc(MjSolution::getUpdateTime);
        List<MjSolution> page = this.list(queryWrapper);
        return page;
    }

    /**
     * 新增数据
     *
     * @param mjSolution 实例对象
     * @return 实例对象
     */
    @Override
    public MjSolution insert(MjSolution mjSolution) {
        this.mjSolutionDao.insert(mjSolution);
        return mjSolution;
    }

    /**
     * 修改数据
     *
     * @param mjSolution 实例对象
     * @return 实例对象
     */
    @Override
    public MjSolution update(MjSolution mjSolution) {
        LambdaQueryWrapper<MjSolution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjSolution::getId,mjSolution.getId());
        this.mjSolutionDao.update(mjSolution,queryWrapper);
        RouterrDto routerrDto=new RouterrDto();
        routerrDto.setId(mjSolution.getId());
        return this.queryById(routerrDto);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.mjSolutionDao.deleteById(id) > 0;
    }
}
